package com.xxxx.seckill.utils;



import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;


/**
 * @Author:王亚栋
 * @Date:2023/2/12 16:43
 * @Version:1.0
 */
@Component
public class MD5Utils {

    public static String md5(String src) {

        return DigestUtils.md5Hex(src);
    }

    private static final String SALT = "1a2b3c4d";

    /**
     * 第一次加密
     * @param inputPass
     * @return
     */
    public static String inputPassToFromPass(String inputPass){

       String str = "" + SALT.charAt(0) + SALT.charAt(2) + inputPass + SALT.charAt(5) + SALT.charAt(4);

       return md5(str);
    }

    /**
     * 第二次加密
     * @param inputPass
     * @return
     */
    public static String fromPassToDBPass(String inputPass,String salt){


        String str = "" + SALT.charAt(0) + SALT.charAt(2) + inputPass + SALT.charAt(5) + SALT.charAt(4);

        return md5(str);
    }

    /**
     * 二次加密后返回的数据库密码
     * @param inputPass
     * @param salt
     * @return
     */
    public static String inputPassToDBPass(String inputPass,String salt){

        String fromPass = inputPassToFromPass(inputPass);

        String dbPass = fromPassToDBPass(fromPass,salt);

        return dbPass;
    }

    public static void main(String[] args) {

        System.out.println(inputPassToFromPass("123456"));
        System.out.println(fromPassToDBPass("d3b1294a61a07da9b49b6e22b2cbd7f9","1a2b3c4d"));
        System.out.println(inputPassToDBPass("123456","1a2b3c4d"));
    }

}
